package me.zk.practice1.hw;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

/**
 * @author zhangkai
 * @date 2023/8/10 11:06
 */
public class HWBJJ35 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int[] nums = Arrays.stream(scanner.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();

        int len = nums.length;

        //复制数组
        int[] doubleNum = Arrays.copyOf(nums,len*2);
        System.arraycopy(nums,0,doubleNum,len,len);
        //结果
        int[] res = new int[len];
        Arrays.fill(res,-100001);

        //放索引
        Deque<Integer> deque = new ArrayDeque<>();
        for (int i = 0; i < doubleNum.length; i++) {
            int num = doubleNum[i];

            while (!deque.isEmpty() && num > nums[deque.peek()]) {
                int index = deque.pop();
                res[index] = num;
            }
            //只放前len个
            if (i < len) {
                deque.push(i);
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < res.length; i++) {
            if (res[i]!= -100001) {
                stringBuilder.append(res[i]);
            } else {
                stringBuilder.append(-1);
            }
            if (i!=res.length-1) {
                stringBuilder.append(",");
            }
        }
        System.out.println(stringBuilder);
    }
}
